﻿using System.Globalization;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Inspections;

namespace NHibernate.Sidekick.Utils.MappingConventions
{
    /// <summary>
    /// Controls how the join tables are named for uni and bi-directional many-to-many's.
    /// </summary>
    public class ManyToManyTableNamingConvention : ManyToManyTableNameConvention
    {
        /// <summary>
        /// Controls how the join table is named for bi-directional relationships ("{MainTable}_{InverseCollection}").
        /// </summary>
        /// <param name="collection">Main collection.</param>
        /// <param name="otherSide">Inverse collection.</param>
        /// <returns>Many-to-many table name.</returns>
        /// <remarks>
        /// This method will be called once per bi-directional relationship; once one side of the relationship
        /// has been saved, then the other side will assume that name aswell.
        /// </remarks>
        protected override string GetBiDirectionalTableName(IManyToManyCollectionInspector collection, IManyToManyCollectionInspector otherSide)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}_{1}",
                                 collection.EntityType.Name, otherSide.EntityType.Name);
        }

        /// <summary>
        /// Controls how the join table is named for uni-directional relationships ("{MainTable}_{ChildTable}").
        /// </summary>
        /// <param name="collection">Main collection.</param>
        /// <returns>Many-to-many table name.</returns>
        protected override string GetUniDirectionalTableName(IManyToManyCollectionInspector collection)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}_{1}",
                                 collection.EntityType.Name, collection.ChildType.Name);
        }
    }
}